//
//  HighlightPropertyView.m
//  WCGradientLayerExample
//
//  Created by 陈晓强 on 2017/5/6.
//  Copyright © 2017年 huangwenchen. All rights reserved.
//

#import "HighlightPropertyView.h"
#import "GradientRingView.h"

#define     kSpeedFromColor     [UIColor colorWithHex:0x2A86CA]
#define     kSpeedToColor       [UIColor colorWithHex:0x2A86CA]

#define     kPowerFromColor     [UIColor colorWithHex:0xEA4947]
#define     kPowerToColor       [UIColor colorWithHex:0xEA4947]

#define     kTimeFromColor      [UIColor colorWithHex:0x49EDA6]
#define     kTimeToColor       [UIColor colorWithHex:0x49EDA6]

#define     kSpeedMaxValue      300
#define     kPowerMaxValue      50
#define     kTimeMaxValue       1000

@interface HighlightPropertyView ()

@property (strong, nonatomic) NSMutableArray *propsView;
@property (strong, nonatomic) NSMutableArray *backsView;

@end

@implementation HighlightPropertyView
- (UIColor *)colorWithHex:(NSInteger)hexValue alpha:(CGFloat)alphaValue
{
    
    return [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0
                           green:((float)((hexValue & 0xFF00) >> 8))/255.0
                            blue:((float)(hexValue & 0xFF))/255.0 alpha:alphaValue];
};
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.propsView = [NSMutableArray array];
        self.backsView = [NSMutableArray array];
        for (int i = 0; i<3; i++) {
            
            CGFloat width = frame.size.width;
            CGFloat lineWidth = 1;
            CGFloat heightCircle = (frame.size.height - lineWidth * 2)/3;
            CGFloat heightBack = heightCircle;

            UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, i*heightBack + i*lineWidth, width, heightBack)];
            backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.7];
            [self addSubview:backView];
            [self.backsView addObject:backView];
            
            NSString *title = i == 0 ? NSLocalizedString(@"Swing Speed", @"速度") : i == 1 ? NSLocalizedString(@"Power", @"力量") : NSLocalizedString(@"Swing time", @"时间");
            NSString *speedUnit = [YFUserDefaults generalMetricSystemUnit] ? NSLocalizedString(@"kph", @"km/h"):@"mph";
            NSString *unit = i == 0 ? speedUnit : i == 1 ? @"N" : @"Ms";
            CGFloat maxValue = i == 0 ? kSpeedMaxValue : i == 1 ? kPowerMaxValue : kTimeMaxValue;
            
            UIColor *toColor = kSpeedToColor;
            UIColor *fromColor = kSpeedFromColor;
            if (i == 1) {
                toColor = kPowerToColor;
                fromColor = kPowerFromColor;
            }else if (i == 2){
                toColor = kTimeToColor;
                fromColor =  kTimeFromColor;
            }
            GradientRingView *ring = [[GradientRingView alloc] initWithFrame:CGRectMake(0, 0, width, heightCircle)
                                                                   fromColor:fromColor
                                                                          to:toColor
                                                                     topText:title
                                                                     midText:@"0"
                                                                     botText:unit
                                                                       value:0
                                                                    maxValue:maxValue];
            [self.propsView addObject:ring];
            [backView addSubview:ring];            
        }
    }
    return self;
}

- (void)animateToValues:(NSArray<NSNumber *> *)values {
    int i = 0;
    for (GradientRingView *view in self.propsView) {
        [view animateToValue:[values[i] floatValue]];
        i++;
    }
}

- (void)configFrame:(CGRect)frame {
    self.frame = frame;
    [self updateFrame];
}

- (void)refreshLayout{
    for (int i = 0; i<self.propsView.count; i++) {
        UIView *propV = self.propsView[i];
        UIView *backV = self.backsView[i];
        [UIView animateWithDuration:.2 animations:^{
            [self setNeedsLayout];
            [propV setNeedsLayout];
            [backV setNeedsLayout];
        }];
    }
}

- (void)updateFrame {
    CGRect frame = self.frame;
     NSMutableArray *propsView = [NSMutableArray array];
    NSMutableArray *backsView = [NSMutableArray array];
    for (int i = 0; i<self.propsView.count; i++) {
        UIView *propV = self.propsView[i];
        UIView *backV = self.backsView[i];
        [propV removeFromSuperview];
        [backV removeFromSuperview];
        CGFloat width = frame.size.width;
        CGFloat lineWidth = 1;
        CGFloat heightCircle = (frame.size.height - lineWidth * 2)/3;
        CGFloat heightBack = heightCircle;
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, i*heightBack + i*lineWidth, width, heightBack)];
        backView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
        [self addSubview:backView];
        [backsView addObject:backView];
        UIColor *fromColor = i == 0 ? kSpeedFromColor : i == 1 ? kPowerFromColor : kTimeFromColor;
        UIColor *toColor = i == 0 ? kSpeedToColor : i == 1 ? kPowerToColor : kTimeToColor;
        
        NSString *title = i == 0 ? NSLocalizedString(@"Swing Speed", @"速度") : i == 1 ? NSLocalizedString(@"Power", @"力量") : NSLocalizedString(@"Swing time", @"时间");
        NSString *speedUnit = [YFUserDefaults generalMetricSystemUnit] ? NSLocalizedString(@"kph", @"km/h"):@"mph";
        NSString *unit = i == 0 ? speedUnit : i == 1 ? @"N" : @"Ms";
        CGFloat maxValue = i == 0 ? kSpeedMaxValue : i == 1 ? kPowerMaxValue : kTimeMaxValue;
        GradientRingView *ring = [[GradientRingView alloc] initWithFrame:CGRectMake(0, 0, width, heightCircle)
                                                               fromColor:fromColor
                                                                      to:toColor
                                                                 topText:title
                                                                 midText:@"0"
                                                                 botText:unit
                                                                   value:0
                                                                maxValue:maxValue];
        [propsView addObject:ring];
        [backView addSubview:ring];            
    }
    self.propsView = propsView;
    self.backsView = backsView;
}
@end
